"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const infoerr_1 = require("./infoerr");
const md5_1 = __importDefault(require("md5"));
//登录验证
const name = /^[A-Za-z][A-Za-z0-9]{2,9}$/;
const pwd = /^[A-Za-z0-9_+-.]{6,16}$/;
const isNum = /^[1-9]{1,4}$/;
exports.default = {
    user(req, res, next) {
        if (!req.body || !req.body.name)
            return (0, infoerr_1.err)(res, "请填写账号");
        if (!req.body || !req.body.pwd)
            return (0, infoerr_1.err)(res, "请填写密码");
        if (name.test(req.body.name) && pwd.test(req.body.pwd)) {
            const body = {
                name: req.body.name,
                pwd: req.body.pwd,
                nickName: req.body.nickName ? req.body.nickName : null,
                imgUrl: req.body.imgUrl ? req.body.imgUrl : null,
            };
            body.pwd = (0, md5_1.default)(body.pwd);
            req.body = body;
            return next();
        }
        return (0, infoerr_1.err)(res, "账号或者密码不符合规范", {
            账号: "字母开头3-9位不能包含除了_以外的字符",
            密码: "由A-Z0-9_+-.组成6-16位",
        });
    },
    limit(req, res, next) {
        if (!req.query || !req.query.x || !req.query.y)
            (0, infoerr_1.err)(res, "没有参数");
        if (isNum.test(req.query.x) && isNum.test(req.query.y))
            return next();
        return (0, infoerr_1.err)(res, "参数错误", { reg: "/^[1-9]{1,4}$/" });
    },
    top(req, res, next) {
        if (!req.query || !req.query.x || !req.query.y || !req.query.z)
            return (0, infoerr_1.err)(res, "参数数量错误");
        const { x, y, z } = req.query;
        if (Object.is(+x, NaN) || Object.is(+y, NaN) || Object.is(+z, NaN))
            return (0, infoerr_1.err)(res, "参数类型错误", { x: "number", y: "number" });
        next();
    },
};
